package com.kuaimacode.kframework.web.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.mybatis.mapper.sysparam.SysParamMapper;
import com.kuaimacode.kframework.mybatis.models.sysparam.SysParam;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class SysParamService extends ServiceImpl<SysParamMapper, SysParam> {

    /**
     *  分页查找所有的数据
     * @param pageNum 当前页码，从1开始
     * @param pageSize  一页显示多少行
     * @return
     */
    public PageInfo<SysParam> findAll(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<SysParam> datas = baseMapper.selectList(null);
        return new PageInfo<SysParam>(datas);
    }

    public SysParam findByName(String name){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name",name);
        return baseMapper.selectOne(queryWrapper);
    }


    /**
     * 根据名称获取值
     *
     * @param name
     * @return
     */
    public String findByNameAndAgent(String name, String agentId) {
        QueryWrapper<SysParam> wrapper = new QueryWrapper<>();
        wrapper.eq("name", name);
        wrapper.eq("agent_id", agentId);
        SysParam sysParam = baseMapper.selectOne(wrapper);
        return Optional.ofNullable(sysParam)
                .map(SysParam::getValue)
                .orElse(null);
    }


}

